Best JavaScript code snippet using wpt
useUsers.ts
Source:useUsers.ts
1import { userKey } from "features/queryKeys";2import { userStaleTime } from "features/userQueries/constants";3import { User } from "proto/api_pb";4import { useCallback, useEffect, useRef } from "react";5import { useQueries, useQueryClient } from "react-query";6import { service } from "service";7import { arrayEq } from "utils/arrayEq";8export default function useUsers(9 ids: (number | undefined)[],10 invalidate = false11) {12 const queryClient = useQueryClient();13 const idsRef = useRef(ids);14 const handleInvalidation = useCallback(() => {15 if (invalidate) {16 queryClient.invalidateQueries({17 predicate: (query) =>18 query.queryKey[0] === userKey() &&19 !!idsRef.current.includes(query.queryKey[1] as number),20 });21 }22 }, [invalidate, queryClient]);23 useEffect(() => {24 handleInvalidation();25 }, [handleInvalidation]);26 //arrays use reference equality, so you can't use ids in useEffect directly27 useEffect(() => {28 if (!arrayEq(idsRef.current, ids)) {29 idsRef.current = ids;30 handleInvalidation();31 }32 });33 const queries = useQueries<User.AsObject, Error>(34 ids35 .filter((id): id is number => !!id)36 .map((id) => ({37 queryFn: () => service.user.getUser(id.toString()),38 queryKey: userKey(id),39 staleTime: userStaleTime,40 }))41 );42 const errors = queries43 .map((query) => query.error?.message)44 .filter((e): e is string => typeof e === "string");45 const isLoading = queries.some((query) => query.isLoading);46 const isFetching = queries.some((query) => query.isFetching);47 // If at least one user query is not loading (i.e. has data loaded before), whilst48 // some other (likely new) queries are fetching, then it's a refetch49 const isRefetching = !queries.every((query) => query.isLoading) && isFetching;50 const isError = !!errors.length;51 const usersById = isLoading52 ? undefined53 : new Map(queries.map((q, index) => [ids[index], q.data]));54 return {55 data: usersById,56 errors,57 isError,58 isFetching,59 isLoading,60 isRefetching,61 };62}63export function useUser(id: number | undefined, invalidate = false) {64 const result = useUsers([id], invalidate);65 return {66 data: result.data?.get(id),67 error: result.errors.join("\n"),68 isError: result.isError,69 isFetching: result.isFetching,70 isLoading: result.isLoading,71 };...
on-update-async.ts
Source:on-update-async.ts
1import { useRef, useEffect } from 'react'2import { EMPTY_OBJECT, NOOP, EMPTY_ARRAY } from 'tsfn'3import { shallowEqualByKeys, unwindGenerator, generatorIdFactory } from './utils'4type TCoroutineProps = {5 cancelOthers: () => void,6 index: number,7}8type GeneratorFactory = (props: TCoroutineProps) => Generator<Promise<any>, void, any>9type RefObject<T> = {10 current: T,11}12export const onUpdateAsync = <P extends {}> (onUpdateFn: (props: RefObject<P>) => GeneratorFactory, watchKeys: (keyof P)[]) => (props: P): P => {13 const useEffectFnRef = useRef<() => void>(NOOP)14 const onUnmountRef = useRef<() => void>(NOOP)15 const propsRef = useRef<P>(EMPTY_OBJECT)16 const watchValuesRef = useRef<any>(EMPTY_ARRAY)17 const isMountedRef = useRef(true)18 const createGeneratorRef = useRef<GeneratorFactory>(NOOP as any)19 const idsRef = useRef<ReturnType<typeof generatorIdFactory>>(EMPTY_OBJECT)20 if (watchValuesRef.current === EMPTY_ARRAY || !shallowEqualByKeys(propsRef.current, props, watchKeys)) {21 watchValuesRef.current = watchKeys.map((k) => props[k])22 }23 propsRef.current = props24 if (useEffectFnRef.current === NOOP) {25 useEffectFnRef.current = () => {26 if (createGeneratorRef.current === NOOP as any) {27 createGeneratorRef.current = onUpdateFn(propsRef)28 }29 if (idsRef.current === EMPTY_OBJECT) {30 idsRef.current = generatorIdFactory()31 }32 const generatorId = idsRef.current.newId()33 unwindGenerator(34 createGeneratorRef.current({35 cancelOthers: idsRef.current.switchToGenerator(generatorId),36 index: generatorId,37 }),38 () => {39 return isMountedRef.current && idsRef.current.isGeneratorRunning(generatorId)40 }41 )42 }43 }44 useEffect(useEffectFnRef.current, watchValuesRef.current)45 if (onUnmountRef.current === NOOP) {46 onUnmountRef.current = () => () => {47 isMountedRef.current = false48 }49 }50 useEffect(onUnmountRef.current, EMPTY_ARRAY)51 return props...
App.js
Source:App.js
1import "./App.css";2import AddSongForm from "./components/AddSongForm/AddSongForm";3import Header from "./components/Header/Header";4import SongList from "./components/SongList/SongList";5import "plyr-react/dist/plyr.css";6import { useRef, useState } from "react";7function App() {8 const idsRef = useRef(1);9 const [songs, setSongs] = useState([]);10 const addSong = (name) => {11 setSongs([...songs, { name, artist: "artist", id: idsRef.current }]);12 idsRef.current++;13 };14 const deleteSong = (id) => {15 setSongs(songs.filter((song) => song.id !== id));16 };17 return (18 <div className="App">19 <Header />20 <AddSongForm onAddSong={addSong} />21 <SongList songs={songs} onDeleteSong={deleteSong} />22 </div>23 );24}...
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org', 'A.1234567890abcdef1234567890abcdef');3wpt.runTest('www.google.com', { location: 'ec2-us-west-1:Chrome', pollResults: 5, video: true }, function(err, data) {4 if (err) console.log(err);5 else {6 console.log(data);7 wpt.getTestResults(data.data.testId, function(err, data) {8 if (err) console.log(err);9 else {10 console.log(data);11 }12 });13 }14});
Using AI Code Generation
1var wptools = require('wptools');2var wiki = wptools.page('Albert Einstein');3wiki.get(function(err, resp) {4 console.log(resp);5});6var wptools = require('wptools');7var wiki = wptools.page('Albert Einstein');8wiki.get(function(err, resp) {9 console.log(resp);10});11var wptools = require('wptools');12var wiki = wptools.page('Albert Einstein');13wiki.get(function(err, resp) {14 console.log(resp);15});16var wptools = require('wptools');17var wiki = wptools.page('Albert Einstein');18wiki.get(function(err, resp) {19 console.log(resp);20});21var wptools = require('wptools');22var wiki = wptools.page('Albert Einstein');23wiki.get(function(err, resp) {24 console.log(resp);25});26var wptools = require('wptools');27var wiki = wptools.page('Albert Einstein');28wiki.get(function(err, resp) {29 console.log(resp);30});31var wptools = require('wptools');32var wiki = wptools.page('Albert Einstein');33wiki.get(function(err, resp) {34 console.log(resp);35});36var wptools = require('wptools');37var wiki = wptools.page('Albert Einstein');38wiki.get(function(err, resp) {39 console.log(resp);40});41var wptools = require('wptools');42var wiki = wptools.page('Albert Einstein');43wiki.get(function(err, resp) {44 console.log(resp);45});46var wptools = require('wptools');
Using AI Code Generation
1var wptools = require('wptools');2var page = wptools.page('Albert Einstein');3page.get(function(err, resp) {4 console.log(resp);5});6var wptools = require('wptools');7### wptools.page(title, options)8var page = wptools.page('Albert Einstein');9### page.get(callback)10page.get(function(err, resp) {11 console.log(resp);12});13### page.json(callback)14page.json(function(err, resp) {15 console.log(resp);16});17### page.html(callback)18page.html(function(err, resp) {19 console.log(resp);20});21### page.full(callback)22page.full(function(err, resp) {23 console.log(resp);24});25### page.lang(lang, callback)26page.lang('es', function(err, resp) {27 console.log(resp);28});29### page.id(id, callback)30page.id('12345', function(err, resp) {31 console.log(resp);32});33### page.idSRef(id, callback)34page.idSRef('12345', function(err, resp) {35 console.log(resp);36});37### page.idText(id, callback)38page.idText('12345', function(err, resp) {39 console.log(resp);40});41### page.idHtml(id, callback)42page.idHtml('12345', function(err, resp) {43 console.log(resp);44});45### page.idFull(id,
Using AI Code Generation
1const wptools = require('wptools')2let page = wptools.page('Barack Obama').get()3page.then(function (result) {4 console.log(result)5})6const wptools = require('wptools')7let page = wptools.page('Barack Obama').id('Q76').get()8page.then(function (result) {9 console.log(result)10})11const wptools = require('wptools')12let page = wptools.page('Barack Obama').ids('Q76', 'Q76').get()13page.then(function (result) {14 console.log(result)15})16const wptools = require('wptools')17let page = wptools.page('Barack Obama').lang('en').get()18page.then(function (result) {19 console.log(result)20})21const wptools = require('wptools')22let page = wptools.page('Barack Obama').lang('en').get()23page.then(function (result) {24 console.log(result)25})26const wptools = require('wptools')27let page = wptools.page('Barack Obama').lang('en').get()28page.then(function (result) {29 console.log(result)30})31const wptools = require('wptools')32let page = wptools.page('Barack Obama').lang('en').get()33page.then(function (result) {34 console.log(result)35})36const wptools = require('wptools')37let page = wptools.page('Barack Obama').lang('en').get()38page.then(function (result) {39 console.log(result)40})41const wptools = require('wptools')42let page = wptools.page('Barack Obama').lang('en').get()43page.then(function (result) {44 console.log(result)45})46const wptools = require('wptools')
Using AI Code Generation
1wptools.idSRef("Q11417").then(function(response) {2 console.log(response);3});4wptools.idSitelinks("Q11417").then(function(response) {5 console.log(response);6});7wptools.idText("Q11417").then(function(response) {8 console.log(response);9});10wptools.idTextSRef("Q11417").then(function(response) {11 console.log(response);12});13wptools.idType("Q11417").then(function(response) {14 console.log(response);15});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!