2.3.5 • Published 10 months ago

@rbxts/shared-components-flamework v2.3.5

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

📈 shared-components-flamework

This package will allow you to create shared components that will synchronize between server and client. This package is reflex-based, so it is important to know how to work with it to understand how this package works.

Example

A code snippet showing you how to create the shared component.

// shared
interface State {
	value: number;
}

@Component()
export class ValueStorageComponent extends SharedComponent<State> {
	protected state = {
		value: 0,
	};
}

// server
@Component({
	tag: "ValueStorageComponent",
})
export class ServerValueStorageComponent extends ValueStorageComponent implements OnStart {
	public onStart() {
		task.spawn(() => {
			while (task.wait(3)) {
				this.increment();
			}
		});
	}

	@Action()
	private increment() {
		return {
			...this.state,
			value: this.state.value + 1,
		};
	}
}

// client
@Component({
	tag: "ValueStorageComponent",
})
export class ClientValueStorageComponent extends ValueStorageComponent {
	@Subscribe((state) => state.value)
	private onIncrement(newValue: number) {
		print(`new value: ${newValue}`);
	}
}

Networking

With this package you can declare remote event, action inside the component, this will allow you to easily make interaction between server and client component

@Component()
export class SomeSharedComponent extends SharedComponent<{}> {
	protected state = {};

	protected remotes = {
		ClientEvent: SharedComponentNetwork.event<ServerToClient, [value: number]>(),
		ServerEvent: SharedComponentNetwork.event<ClientToServer, [value: number]>(),
		Action: SharedComponentNetwork.action<[value: number], void>(),
	};
}

// server
@Component({
	tag: "SomeSharedComponent",
})
export class ServerComponent extends SomeSharedComponent implements OnStart {
	public onStart() {
		this.remotes.ServerEvent.Connect((player, amount) => {
			print(`value = ${amount}, player: ${player}`);
		});

		this.remotes.Action.OnRequest((amount) => {
			print(`Action: value = ${amount}`);
		});

		task.wait(5);
		this.remotes.ClientEvent.Broadcast(1);
	}
}

// client
@Component({
	tag: "SomeSharedComponent",
})
export class ClientComponent extends SomeSharedComponent implements OnStart {
	public onStart() {
		this.remotes.ClientEvent.Connect((amount: number) => {
			print(`value = ${amount}`);
		});
		this.remotes.ServerEvent.Fire(1);
		this.remotes.Action(1);
	}
}

API

  • SharedComponent<S, A, I>

The base class of any sharedComponent. Accepts three generics("S" - type describing the state of the component, "A" - attributes of the component, "I" - type of instance of the component)

  • @Action()

Decorator turns your method into action. (action is a function that changes the state of your component).

  • @Subscribe(selector, predicate?)

This decorator subscribes your method to listen for a state change.

2.3.5

10 months ago

2.2.1

1 year ago

2.2.0

1 year ago

2.2.3

1 year ago

2.2.2

1 year ago

2.2.5

1 year ago

2.2.4

1 year ago

2.2.7

12 months ago

2.2.6

1 year ago

2.1.9

1 year ago

2.3.0

12 months ago

2.1.2

1 year ago

2.3.2

11 months ago

2.1.4

1 year ago

2.3.1

11 months ago

2.1.3

1 year ago

2.3.4

10 months ago

2.1.6

1 year ago

2.3.3

11 months ago

2.1.5

1 year ago

2.1.8

1 year ago

2.1.7

1 year ago

2.2.8

12 months ago

2.1.1

1 year ago

2.0.9

1 year ago

2.1.0

1 year ago

2.0.5

1 year ago

2.0.4

1 year ago

2.0.7

1 year ago

2.0.6

1 year ago

2.0.8

1 year ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.41

1 year ago

1.2.42

1 year ago

1.2.45

1 year ago

1.2.46

1 year ago

1.2.43

1 year ago

1.2.44

1 year ago

1.2.52

1 year ago

1.2.53

1 year ago

1.2.51

1 year ago

1.2.54

1 year ago

1.2.2

1 year ago

1.2.12

1 year ago

1.2.13

1 year ago

1.2.0

1 year ago

1.2.1

1 year ago

1.2.11

1 year ago

1.1.9

1 year ago

1.1.81

1 year ago

1.1.85

1 year ago

1.1.84

1 year ago

1.1.83

1 year ago

1.1.82

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.0.0

1 year ago