Giter Club home page Giter Club logo

ttpg-common-lib's Introduction

ttpg-common-lib

pnpm add ttpg-common-lib

yarn add ttpg-common-lib

npm i ttpg-common-lib

Storage

import { refObject } from "@tabletop-playground/api";
import { Storage } from "ttpg-common-lib";

type MyStore = {
    someKey: string;
};

// format of the storage id is enforced by typescript to help avoid collisions.
const databank = Storage.get<MyStore>(refObject, "@ThatRobHuman/myStoreId");

// will be undefined if not found, hence null coalescence.
const store = databank.load() ?? { someKey: "defaultValue" };

databank.save({ someKey: "newValue" });

EventBus

import { GameObject } from "@tabletop-playground/api";
import { EventBus } from "ttpg-common-lib";

// schema of your event bus: key is the "signal", values are vargs for any handler that a handler would take.
type MyEvents = {
    someEvent: [GameObject];
};

// format of the event bus id is enforced by typescript to help avoid collisions.
const bus = EventBus.get<MyEvents>("@ThatRobHuman/myChannelId");

const someHandler = (obj: GameObject) => {
    // do stuff
};

// returns an unsubscribe function
const unsub = bus.subscribe("someEvent", someHandler);

// that you can call to remove the handler
unsub();

// or you can remove it the old-fashioned way
bus.unsubscribe("someEvent", someHandler);

// remove all handlers for this signal
bus.clear("someEvent");

CrossRef

cross ref lets you drop off and pick up data points based on key - think of it as a global non-persistant key-value store, however: it's probably most useful as an object reference store.

import { GameObject } from "@tabletop-playground/api";
import { CrossRef } from "ttpg-common-lib";

/* Types.ts */

type Schema = {
    foo: string;
}

/* objectA */

((obj: GameObject) => {

    const utilityData: Schema = {
        foo: "bar";
    }

    const unregister = CrossRef.register<Schema>("@ThatRobHuman/myUtility", obj.getId(), utilityStore);

    obj.onDestroyed.add(() => {
        unregister(); //remove when object is destroyed.
    })

})(refObject)

/* objectB */

((obj: GameObject) => {

    const otherObjectId = "asdf"; //whatever

    obj.onCustomAction.add(() => {
        const thing = CrossRef.get<Schema>("@ThatRobHuman/myUtility", otherObjectId); // might be undefined.
        if (thing) {
            console.log(thing.foo) // "bar"
        }
    })

})

ttpg-common-lib's People

Contributors

robmayer avatar

Stargazers

Omniraptor avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.