npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cs-event-client

v0.3.0

Published

A light-weight Websockets-wrapper class for stateful event management.

Downloads

1

Readme

CodeScape Event Client

A Vuex module to expose the CodeScape Event Socket in any Vue component.

Installation

npm i -s cs-event-client

Example Usage

Configuring the environment

// main.js
import { createApp } from 'vue';
import App from './App.vue';
import store from './store';

createApp(App).use(store).mount('#app');
// store/index.js
import { createStore } from 'vuex';
import { getVuexSocket } from 'cs-event-client';

const websocketURL = 'ws://localhost:3000/ws/';

const vuexSocket = getVuexSocket(websocketURL);

export default createStore(vuexSocket);

If the component has its own, additional Vuex Store states, getters, mutations, actions, and modules, you can specify these using the .extend syntax:

// store/index.js
import { createStore } from 'vuex';
import { getVuexSocket } from 'cs-event-client';

const websocketURL = 'ws://localhost:3000/ws/';

const vuexSocket = getVuexSocket(websocketURL).extend({
    state: {
        customProp: [],
    },
    getters: {
        customGetter: () => 42, 
    },
    mutations: {
        customMutation: (state) => {
            state.customProp.push('data');
        },
    },
    actions: {
        customAction: ({ commit }) => {
            commit('customMutation');
        },
    },
});

export default createStore(vuexSocket);

More information about states, getters, mutations, actions, and modules can be found on the Vuex website.


If the Vue application you're making will be responding instantaneously to new events in the game, there is additional setup required.

Firstly, you'll need to use the codescapePlugin, which exposes the $csEvent global property (similar to this.$store, there is now this.$csEvent).

// main.js
import { createApp } from 'vue';
import { codescapePlugin } from 'cs-event-client';

import App from './App.vue';
import store from './store';

createApp(App)
    .use(store)
    .use(codescapePlugin)
    .mount('#app');

You'll also need to load the CodeScape mixin into the root Vue component (usually 'App.vue') which will watch for any activity in the Game.

// App.vue
<template>
    <router-view/>
</template>

<script>
import { codescapeMixin } from 'cs-event-client';

export default {
    mixins: [codescapeMixin],
};
</script>

Connecting to a game

Declaring or listening for events cannot be done until the application has been registered to a game. Registering to a game only needs to be done once per application instance using the 'registerGame' action. It can be called from any component.

registerGame will connect the Vue app to a given game's event stream. Notice that the second argument must be an object:

this.$store.dispatch('registerGame', { 
    gameID, debug 
});

gameID (string) specifies the unique ID for the game to connect to.

debug (boolean) if set to true, all messages received will be printed to the console as a debug message.

Example usage is below:

// components/Example.vue
export default {
    created() {
        this.$store.dispatch('registerGame', { 
            gameID: 'anything',
        });
    },
};

Declaring events

Declaring an event to the game server is also done via the Store using the 'declareEvent' action.

declareEvent will declare an event to the game stream the store is connected to.

this.$store.dispatch('declareEvent', { 
    ID, status 
});

ID (int) specifies the unique ID of the event that is being declared.

status (int) specifies the unique status code of the event that is being declared.

Listening for events

Listening for events can either be done via callback or checking for past events.

If real-time response to an event is preferred, you can register a function as a callback to a given event using the $csEvent.register method (providing you have connected the plugin and mixin as described above).

this.$csEvent.register(eventID, callbackFn);

eventID (int) specifies the unique ID of the event that is being watched for.

callbackFn (func) specifies the callback function to be executed when the chosen event occurs on the game stream. The status code of the event will be passed into this function as the first (and only) argument.

export default {
    created() {
        this.$csEvent.register(1, this.logEvent);
    },
    methods: {
        logEvent1(status) {
            console.log(status);
        },
    },
};

Alternatively, to see the current status of an event - or all of its previous statuses - the eventDirectory object is used to get information about events that have occurred in the game.

To access it from any component, link the component to the eventDirectory's store entry:

// components/Example.vue
import { mapGetters } from 'vuex';

export default {
    name: 'Example',
    computed: {
        ...mapGetters([
            'eventDirectory',
        ]),
    },
    methods: {
        accessEventDirectory() {
            return this.eventDirectory.getAllEventStatuses();
        },
    },
};

The eventDirectory has five methods:

eventHasOccurred(ID) will return a boolean indicating whether the event with ID has occurred (i.e. its status is != 0).

getEventStatus(ID) will return the latest event status for the event with ID.

getEventHistory(ID) will return a chronologically-ordered array of all event statuses that have occurred for the event with ID.

getAllEventStatuses() will return an object containing ID-status key-value pairs for all recorded events.

getAllEventHistories() will return an object containing ID-history key-value pairs for all recorded events.