Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
setTimeout(() => useStateLinkUnmounted(stateInf)
.set(tasks => tasks.concat([{ name: 'Second task by timeout', priority: 1 }]))
, 5000) // adds new task 5 seconds after website load
setInterval(() => useStateLinkUnmounted(store) // get to the state of the object
.nested.counter // get to the state of the counter property
.set(p => p + 1) // increment the counter...
, 3000) // ...every 3 seconds
interface ServiceMessageLeaderId {
readonly version: number;
readonly kind: 'leader-elected';
readonly id: number;
}
type ServiceMessage = ServiceMessageInitialRequest | ServiceMessageLeaderId;
const PluginID = Symbol('Broadcasted');
class BroadcastedPluginInstance implements PluginInstance {
private broadcastRef: BroadcastChannelHandle;
private isBroadcastEnabled = true;
private statusStateRef = createStateLink({ isLoading: true, isLeader: false });
private statusState = useStateLinkUnmounted(this.statusStateRef);
private instanceId = -1;
constructor(
readonly topic: string,
readonly unmountedLink: StateLink,
readonly onLeader?: () => Promise
) {
this.broadcastRef = subscribeBroadcastChannel(topic, (message: BroadcastMessage | ServiceMessage) => {
if (message.version > 1) {
// peer tab has been upgraded
return;
}
if ('kind' in message) {
if (this.statusState.value.isLeader && message.kind === 'request-initial') {
this.broadcastRef.channel.postMessage({
version: 1,
setTimeout(() => useStateLinkUnmounted(stateRef)
.set(tasks => tasks.concat([{ name: 'Second task by timeout', priority: 1 }]))
, 5000) // adds new task 5 seconds after website load
setInterval(() => useStateLinkUnmounted(store)
.nested[0] // get to the state of the first array element
.nested.counter // get to the state of the element's counter
.set(p => p + 1) // increment the counter...
, 3000) // ...every 3 seconds
import React from 'react';
import { createStateLink, useStateLink, useStateLinkUnmounted } from '@hookstate/core';
const store = createStateLink(0);
useStateLinkUnmounted(store);
export const ExampleComponent = () => {
const state = useStateLink(store);
return <>{state.value}
}
setInterval(() => useStateLinkUnmounted(stateRef).set(p => p + 1), 3000)
setInterval(() => useStateLinkUnmounted(store).nested[0].set(p => p + 1), 3000)