Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
@ircam/shared-state
Advanced tools
> Library dedicated at maintaining state(s) synchronized accross nodes > of a network. > The library doesn't implement any transport method, however it should > work out of the box with popular socket libraries that implement an > `EventEmitter` API (e
Readme
shared-state
Library dedicated at maintaining state(s) synchronized accross nodes of a network. The library doesn't implement any transport method, however it should work out of the box with popular socket libraries that implement an
EventEmitter
API (e.g.socket.io
, or a simpleEventEmitter
interface
on top ofWebSocket
)
npm install @ircam/shared-state --save
import { Server } from '@ircam/shared-state';
const server = new Server();
server.registerSchema('my-schema', {
myInteger: {
type: 'integer',
default: 0,
},
// ....
});
// `clientId` - unique id of a client
// `transport` - some transport (WebSocket, EventEmitter) implementing a basic
// event API (i.e. `emit`, `addListener`, `removeAllListeners`)
server.addClient(clientId, transport);
const state = await server.create(`my-schema`);
await state.set({ myInterger: 2 })
state.getValues();
// > { myInteger: 2 }
import { Client } from '@ircam/shared-state';
const client = new Client(nodeId, transport);
const state = await client.attach('my-schema');
state.subscribe(updates => {
// > updates = { myInteger: 2 }
});
StateManager
APICreate a synced my-state
state where owner
is the server and creator
is the client
const state = await stateManager.create('my-schema');
Create a local my-state
state where owner
and creator
are owner
@important - the state is purely local then no other node can attach to it
const state = await stateManager.create('my-schema', false);
Attach to a state created by another client
const state = await stateManager.attach('my-schema', clientId);
client-side only: attach to a state created by the server
const state = await stateManager.attach('my-schema');
Observe the creation of new states, the callback is called a first time with a list of all already created states, and then each time a new state is created.
// @note - unroll first set in the stateManager itself
stateManager.observe((schemaName, nodeId) => {
// the node is interested in 'whatever' schemaName
if (schemaName === 'whatever') {
const state = await stateManager.attach(schemaName, nodeId);
// do things with `state`
}
}
});
StateManager
API@note: by convention the server nodeId
is -1
State
APIA state cannot be instanciated manually, its always created using the StateManager
factory methods create
or attach
.
Returns the schema of the state as registered in the server.
const schema = state.getSchema();
Update state with new values. Given values may be ignored or sanitized according to the definition of the parameter.
// given the schema
// {
// myInt: {
// type: 'integer',
// min: 0,
// max: 10,
// }
// }
const updated = await state.set({ myInt: -4 })
// updated.myInt === 0
Return the current value of the name
state entry.
Return the current values of the state as flat <key, value> object
Subscribe to modifications of the state. Return a function that allow to unsubscribe the listener.
const unsubscribe = state.subscribe(updates => {
for (let key in updates) {
switch (key) {
// ...
}
}
doSomethingWithAll(state.getValues());
});
// ...later
unsubscribe();
Detach from the state:
Calling detach immediatly removes all listeners subscribed to the state, then requests the server (owner of the state) for detaching. Calls the onDetach
listeners when acknoledgement is received from the server.
state.detach();
state.onDetach(() => {
// clean everything related to the state
});
BSD-3-Clause
FAQs
> Library dedicated at maintaining state(s) synchronized accross nodes > of a network. > The library doesn't implement any transport method, however it should > work out of the box with popular socket libraries that implement an > `EventEmitter` API (e
The npm package @ircam/shared-state receives a total of 2 weekly downloads. As such, @ircam/shared-state popularity was classified as not popular.
We found that @ircam/shared-state demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.